﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MMPS.Entity.POCOMdel;
using MMPS.Common;
using MMPS.Entity.ViewModel;
using System.Linq.Expressions;

namespace MMPS.Business
{
   public  class UserServe:BaseService<Users>
    {
        #region  查询
        public OperateResult GetUserList(UserPageViewModel viewModel)
        {
            var query = PredicateExtensions.True<Users>();
            //判断条件
            if (!string.IsNullOrWhiteSpace(viewModel.U_LoginName))
            {
                query = query.And(u => u.U_LoginName == viewModel.U_LoginName);
            }
            if (!string.IsNullOrWhiteSpace(viewModel.U_RealName))
            {
                query = query.And(u => u.U_LoginName == viewModel.U_RealName);
            }
            if (!string.IsNullOrWhiteSpace(viewModel.U_Telephone))
            {
                query = query.And(u => u.U_LoginName == viewModel.U_Telephone);
            }

            //设置初始总条数为0
            int rowsCount = 0;
            //执行查询
            var pageDate = GetList(viewModel.page, viewModel.rows, ref rowsCount, query, u => u.U_ID, true).Select(u => new UserViewModel
            {
                U_ID = u.U_ID,
                U_CanDelete = u.U_CanDelete==false?"否":"是",
                U_LoginName = u.U_LoginName,
                U_RealName = u.U_RealName,
                U_Role =((RoleTypeEnum)u.U_Role).ToString(),
                U_Sex = u.U_Sex,
                U_Telephone = u.U_Telephone
            }).ToList();
            //设置返回的数据
            var pageViewModel = new DatgRIDViewModel { total = rowsCount, rows = pageDate };
            //返回值,查询出来的结果，和总条数
            return new OperateResult(ResultStatus.Success, "", pageViewModel);

        }
        #endregion

        #region   添加
        public OperateResult UserADD(UserListViewModel viewModel)
        {
            OperateResult result = null;
            if (Find(u=>u.U_LoginName==viewModel.U_LoginName)!=null)
            {
                result = new OperateResult(ResultStatus.Success, "该登录名已被注册过！");
            }
            else
            {//页面传过来的值是UserListViewModel模型的，需要转成User
                Users users = new Users();
                users.U_LoginName = viewModel.U_LoginName;
                users.U_RealName = viewModel.U_RealName;
                users.U_Sex = viewModel.U_Sex;
                users.U_Telephone = viewModel.U_Telephone;
                users.U_Role = (int)viewModel.U_Role;
                users.U_CanDelete = viewModel.U_CanDelete == "否" ? false : true;
                //写添加   调用MMPS.Business里的Add()方法
                bool res = Add(users);

                if (res)
                {
                    result = new OperateResult(ResultStatus.Success, "添加成功！");
                }
                else
                {
                    result = new OperateResult(ResultStatus.Success, "添加失败！");
                }
            }

            return result;
        }
        #endregion


        #region   根据id查询对应数据，进行修改
        public OperateResult UserListUpdate(int id)
        {
            //根据页面传过来的id判断数据是否存在
            //Users users = new Users();
            var rest = Find(u => u.U_ID == id);
            //把user转化成viewmodel
            UserListViewModel userviewModel = new UserListViewModel();
            if (rest != null)
            {
                userviewModel.U_ID = rest.U_ID;
                userviewModel.U_CanDelete = rest.U_CanDelete==false?"否":"是";
                userviewModel.U_LoginName = rest.U_LoginName;
                userviewModel.U_RealName = rest.U_RealName;
                userviewModel.U_Role =(RoleTypeEnum)rest.U_Role;
                userviewModel.U_Sex = rest.U_Sex;
                userviewModel.U_Telephone = rest.U_Telephone;
            }

            //把查询的结果 放到 data里面 返回data,userviewModel就是OperateResult(放成功或失败，“文本信息”，data)的data
            return new OperateResult(ResultStatus.Success, "", userviewModel);
        }
        #endregion

        #region  执行修改

        public OperateResult UserUpdate(UserListViewModel viewModel)
        {
            //先根据传的id判断值是否存在
            var rest = Find(u => u.U_ID == viewModel.U_ID);
            OperateResult result = null;
            if (rest != null)
            {
                //页面传过来的值是UserListViewModel模型的，需要转成User
                rest.U_LoginName = viewModel.U_LoginName;
                rest.U_RealName = viewModel.U_RealName;
                rest.U_Sex = viewModel.U_Sex;
                rest.U_Telephone = viewModel.U_Telephone;
                rest.U_Role =Convert.ToInt32(viewModel.U_Role);
                rest.U_CanDelete = viewModel.U_CanDelete=="否"?false:true;
                //写修改  调用MMPS.Business里的Update()方法
                bool res = Update(rest);
                if (res)
                {
                    result = new OperateResult(ResultStatus.Success, "修改成功！");
                }
                else
                {
                    result = new OperateResult(ResultStatus.Success, "修改失败！");
                }

            }
            else
            {
                result = new OperateResult(ResultStatus.Success, "该用户不存在！");
            }

            return result;
        }
        #endregion

        #region  删除
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="viewModel"></param>
        /// <returns></returns>
        public OperateResult UserDelete(UserPageViewModel viewModel)
        {
            //先查询
            var res = Find(u => u.U_ID == viewModel.U_ID);
            OperateResult result = null;
            if (res != null)
            {
                if (res.U_CanDelete==true)
                {
                    //后删除  调用MMPS.Business里的Delete()方法
                    bool list = Delete(res);
                    if (list)
                    {
                        result = new OperateResult(ResultStatus.Success, "删除成功！");
                    }
                    else
                    {
                        result = new OperateResult(ResultStatus.Error, "删除失败！");
                    }
                }
                else
                {
                    result = new OperateResult(ResultStatus.Error, "该用户不能被删除！");
                }
               
            }
            else
            {
                result = new OperateResult(ResultStatus.Error, "该用户不存在！");
            }
            return result;

        }
        #endregion

    }
}
